package name.jiaosha.air.user.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.OperateTip;
import name.jiaosha.air.company.domain.Company;
import name.jiaosha.air.company.service.ICompanyService;
import name.jiaosha.air.user.domain.LoginUser;
import name.jiaosha.air.user.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.AliUtil;

@Controller
public class LoginUserController {
	
	private IUserService userService;

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("/admin/user/loginUserList")
	public ModelAndView getMyLoginUserList() {

		List<LoginUser> list = null;

		if (AppConstants.DEBUG) {
			list = new ArrayList<LoginUser>();
			LoginUser user = new LoginUser();
			user.setId(1);
			user.setName("徐声耀");
			user.setQq("2314999");
			user.setEnabled(true);
			list.add(user);

			user = new LoginUser();
			user.setId(2);
			user.setName("黄新");
			user.setQq("d1499d9");
			user.setEnabled(false);
			list.add(user);

		} else {

			list = userService.getLoginUserListByComId(1);
		}
		ModelMap model = new ModelMap();
		model.addAttribute("root", list);
		return new ModelAndView("jsonView", model);

	}

	public OperateTip create(LoginUser loginUser) {

		loginUser.setPassword(AliUtil.MD5(loginUser.getPassword()));
		
		userService.createLoginUser(loginUser);
		OperateTip tip = new OperateTip("1", "操作成功");
		return tip;
	}

	public LoginUser getLoginUserById(int id) {

		return userService.getLoginUserById(id);

	}

	public OperateTip update(LoginUser loginUser) {

		LoginUser user = userService.getLoginUserById(loginUser.getId());

		if (loginUser.getPassword() != null
				&& loginUser.getPassword().length() >= 6) {

			user.setPassword(AliUtil.MD5(loginUser.getPassword()));
		}
		userService.updateLoginUser(user);
		OperateTip tip = new OperateTip("1", "操作成功");
		return tip;
	}
}
